package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;


import javax.servlet.http.HttpSession;

/*
 *@AUTHOR:LIUCHAO;
 *@DATE: 2020/9/13 9:06
 */
@RequestMapping("/validateCode")
@RestController
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Login")/*发送验证码*/
     public Result send4Login(String telephone){
        Integer integer = ValidateCodeUtils.generateValidateCode(4);
        String code = integer.toString();
        //将生成的验证码缓存到redis/*此验证码有时效性*/
        jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_ORDER,5 * 60,code);
        jedisPool.getResource().setex("order"+telephone,5 * 60,telephone);//TODO  修改预约的redis中的key


//        jedisPool.getResource().sadd("telephone",telephone);
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
//        Map data=new HashMap< >();
//        data.put("telephone",telephone);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send6Login")/*发送验证码*/
    public Result send6Login(String telephone){
        Integer integer = ValidateCodeUtils.generateValidateCode(6);
        String code = integer.toString();
        //将生成的验证码缓存到redis/*此验证码有时效性*/
        jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,5 * 60,code);
        jedisPool.getResource().setex("login"+telephone,5 * 60,telephone);

        try {
            SMSUtils.sendShortMessage(SMSUtils.LOGIN_CODE,telephone,code);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }



}
